<?php
	include_once("inc/constants.inc.php");
	include_once("inc/connection.php");
	session_start();
	
if(!isset($_SESSION['login']) || empty($_SESSION['login']) || !isset($_SESSION['email']) || empty($_SESSION['email']) || !isset($_SESSION['user_role']))
	{
		header("Location: login.php?error=invalid-access");
		exit(0);
	}
elseif($_SESSION['user_role']==2)
	{
		header("Location: test.php?error=invalid-access");
		exit(0);
	}

	if(isset($_POST['test_type']))
		$test_type = $_POST['test_type'];
	if(isset($_GET['test_type']))
		$test_type = $_GET['test_type'];
		
	$action = "";
	if(isset($_POST['action']))
		$action = $_POST['action'];
	if(isset($_GET['action']))
		$action = $_GET['action'];
		
	$result = mysql_query("SELECT * FROM question_type WHERE QTypeID='$test_type'");	
	while($row = mysql_fetch_array($result))
	{
		$question_type = $row['QType'];
	}
	
	$update=false;

	if(isset($_GET['questionId']))
	{
		$questionId = $_GET['questionId'];
		$query = mysql_query("SELECT * FROM question_table WHERE questionNumber='$questionId'");	
		while($row = mysql_fetch_array($query))
		{ 	
			$question = $row['question'];
			$correctOption = $row['correctOption'];
			$correctAnswer = $row['correctAnswer'];
			$optionOne = $row['optionOne'];
			$optionTwo = $row['optionTwo'];
			$optionThree = $row['optionThree'];
			$optionFour = $row['optionFour'];
			$update = true;
		}
	}

?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8"> 
		<title>Smartek online test | Add Question</title>
		<link rel="SHORTCUT ICON" href="img/disp/favicon.png" />	
		<link rel="stylesheet" href="css/bootstrap.css"  type="text/css"/>
		<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
		<script src="tinymce/tinymce.min.js"></script>
		<script>
			tinymce.init({selector:'textarea'});
		</script>
		<script>
			function validate()
			{
				
				
				var error = 0;
				var check = $('input[name=optionRadio]:checked').length;
				if(check==0)
				{	
					error=1;
					alert("Please select the correct option");
					
				}

				if($('input[name=question]').val()=='')
				{
					error=1;
					$('#questionError').html("Question is missing");
				}
				if($('input[name=optionOne]').val()=='')
				{
					error=1;
					$('#option1Error').html("First option is missing");
				}
				if($('input[name=optionTwo]').val()=='')
				{
					error=1;
					$('#option2Error').html("Second option is missing");
				}
				if($('input[name=optionThree]').val()=='')
				{
					error=1;
					$('#option3Error').html("Third option is missing");
				}
				if($('input[name=optionFour]').val()=='')
				{
					error=1;
					$('#option4Error').html("Fourth option is missing");
				}
				
				if(error)
				alert("Please enter all the fields");
				
				if(error==0)				
					return true;
				else
					return false;
				
				
				
			}
			
			function deleteConfirm()
			{
				if(confirm("Are you sure you want to delete"))
				return true;
			}
			
		</script>
		
	</head>
	<body>
		
		<script src="js/bootstrap.js"></script>
		
		<div class="container">
			<a href="index.php"><img src="img/disp/logo.png" alt=""></a>
		</div>
		
		<div class="container">
			<div class="row pull-right">
				<div class="span4 pull-right" >
					<div class="pull-right">
						<?php 
						if(!isset($_SESSION['name']) || empty($_SESSION['name'])) echo  "<a href='index.php'>Register</a>";
						if(isset($_SESSION['name']) && !empty($_SESSION['name']) && isset($_SESSION['user_role']) && $_SESSION['user_role']==1) echo  "Welcome ".$_SESSION['name']."! | <a href='dashboard.php'>Dashboard</a> | <a href='logout.php'>Log out</a>"; 
						if(isset($_SESSION['name']) && !empty($_SESSION['name']) && isset($_SESSION['user_role']) && $_SESSION['user_role']==2) echo  "Welcome ".$_SESSION['name']."! | <a href='dashboard.php'>Continue test</a>"; 
						?>
					</div>
				</div>
			</div>
		</div>
		<div class="container">
			<hr class="featurette-divider">
		</div>
		<div class="container">
						<?php if($action !='view'){ ?>
						<a href="show-question-list.php">View Question List</a>
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<?php } else { ?>
						<a href="select-type-admin.php">Add question</a> 
							&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<?php } ?>
						<a href="manage-time.php">Manage Exam Time</a><br/><br/>
		</div>

		<div class="container">
			<div class="row">
				<div class="span12" > <!--pagination-centered-->
					<?php
						$result = mysql_query("SELECT * FROM question_type WHERE QTypeID='$test_type'");	
						while($row = mysql_fetch_array($result))
						{
							echo "<h4>" . $row['QType'] ." Section</h4>";
						}
						if(isset($_GET['isAdded']) && $_GET['isAdded']=="yes")
							{
								echo "<font color='blue'>Added Successfully!</font><br/>";
							}
						if(isset($_GET['isEmpty']))
							{
								echo "<font color='red'>All Fields are mendatory</font><br/>";
							}
						if(isset($_GET['isEmptyCheck']))
							{
								echo "<font color='red'>Select the correct answer</font><br/>";
							}
						if(isset($_GET['isUpdated']) && $_GET['isUpdated']=="yes")
							{
								echo "<font color='blue'>Updated Successfully</font><br/>";
							}
						if(isset($_GET['isDeleted']) && $_GET['isDeleted']=="yes")
							{
								echo "<font color='blue'>Deleted Successfully</font><br/>";
							}	
					?>
					
					<?php
						if($action=='update' || $action=='add')
						{
						?>
					<form action="process/add-update.php" onSubmit="return validate()" method="POST">
						</br>Please enter the question here</br>
						<textarea style="resize:vertical; width:930px; height: 100px" name="questionTextArea"><?php if(isset($question)) echo $question; ?></textarea>		
						</br>
						Please enter the options and select the correct answer</br>
						<div style="width:700px; margin:left">
							<input type="radio" name="optionRadio" <?php if(isset($correctOption) && $correctOption=='a') echo "checked";  ?> value="a"> A. 
							<textarea style="resize:vertical;" name="optionOne"><?php if(isset($optionOne)) echo $optionOne; ?></textarea>
							<br/>
							<input type="radio" name="optionRadio" <?php if(isset($correctOption) && $correctOption=='b') echo "checked";  ?> value="b"> B. 
							<textarea style="resize:vertical;" name="optionTwo"><?php if(isset($optionTwo)) echo $optionTwo; ?></textarea>
							<br/>
							<input type="radio" name="optionRadio" <?php if(isset($correctOption) && $correctOption=='c') echo "checked";  ?> value="c"> C. 
							<textarea style="resize:vertical;" name="optionThree"><?php if(isset($optionThree)) echo $optionThree; ?></textarea>
							<br/>
							<input type="radio" name="optionRadio" <?php if(isset($correctOption) && $correctOption=='d') echo "checked";  ?> value="d"> D. 
							<textarea style="resize:vertical;" name="optionFour"><?php if(isset($optionFour)) echo $optionFour; ?></textarea>
						</div>
			<?php /* ?>
						<input type="radio" name="optionRadio" <?php if(isset($correctOption) && $correctOption=='a') echo "checked";  ?> value="a"> A. <input type="text" class="input-xxlarge"  name="optionOne" value="<?php if(isset($optionOne)) echo $optionOne; ?>"><br/>
						<input type="radio" name="optionRadio" <?php if(isset($correctOption) && $correctOption=='b') echo "checked";  ?> value="b"> B. <input type="text" class="input-xxlarge" name="optionTwo" value="<?php if(isset($optionTwo)) echo $optionTwo; ?>"><br/>
						<input type="radio" name="optionRadio" <?php if(isset($correctOption) && $correctOption=='c') echo "checked";  ?> value="c"> C. <input   type="text" class="input-xxlarge" name="optionThree" value="<?php if(isset($optionThree)) echo $optionThree; ?>"><br/>
						<input type="radio" name="optionRadio" <?php if(isset($correctOption) && $correctOption=='d') echo "checked";  ?> value="d"> D. <input   type="text" class="input-xxlarge" name="optionFour" value="<?php if(isset($optionFour)) echo $optionFour; ?>"><br/>
			<?php */ ?>
						<input type="hidden" name="test_type" value="<?php echo $test_type; ?>">
						<p align="right">
							<input type="hidden" name="hiddenQuestionId" value="<?php if(isset($questionId)) echo $questionId ; ?>">
							<input type="hidden" name="hiddenId" value="<?php //echo $typeId; ?>">
							<input type="hidden" name="hiddenUserName" value="<?php //echo $userName; ?>">
							<?php
							if(isset($update) && $update)
							{
								echo "<input type='hidden' name='test_type' value=".$test_type.">";
								echo "<input type='submit' name='update' class='btn' value='Update'>";
							}
							else
									echo "<input type='submit' name='add' class='btn' value='Save'>";
							?>
							
						<input type="reset" name="reset" class="btn" value="Reset">
						</p>
					</form>
					
					<?php } ?>
	
					
					<form action="" method="GET">
						<?php	
							$serial_no = 0;
							$result = mysql_query("SELECT * FROM question_table WHERE typeId='$test_type'");		
							while($row = mysql_fetch_array($result))
							{	
								//echo ++$serial_no.". ";
								//echo $row['questionNumber'] . ".";
								echo "<a name='$row[questionNumber]'></a>";
								echo "<b>" . ++$serial_no.". ".$row['question'] . "</b><br/><br/>" ;
								echo "<b>A. </b> " . $row['optionOne'] . "<br/>" ;
								echo "<b>B. </b> " . $row['optionTwo'] . "<br/>" ;
								echo "<b>C. </b> " . $row['optionThree'] . "<br/>" ;
								echo "<b>D. </b> " . $row['optionFour'] . "<br/>" ;
								echo "<b>Correct Answer : </b>" . $row['correctAnswer'] . "<br/><br/>" ;
								echo "<input type='hidden' name='hiddenQuestionId' value='$row[questionNumber]'>";
								echo "<a href='add-update.php?action=update&questionId=$row[questionNumber]&test_type=$test_type'>Edit</a>&nbsp;&nbsp;&nbsp;
									<a href='process/add-update.php?action=delete&delete=yes&questionId=$row[questionNumber]&test_type=$test_type' onclick=\"return confirm('Are you sure you want to delete?')\">Delete</a>" . "<br/><br/>" ;
							}
						?>		
					</form>
				</div>
			</div>
		</div>

		
		<div class="container marketing">
		<!-- FOOTER -->
		<div class="container">
			<hr class="featurette-divider">
		</div>
			  <footer>
				<p>&copy; 2014 SCS &middot; </p>
			  </footer>

		</div><!-- /.container -->
	</body>
</html>